package com.ponhu.ea2.stockcenter.provider.entity;

import com.baomidou.mybatisplus.annotation.*;

import java.io.Serializable;
import java.util.Date;

import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ObjectUtils;

/**
 * 实物类库存表
 *
 * @TableName ph_stock_inventory_real
 */
@Accessors(chain = true)
@TableName(value = "ph_stock_inventory_real")
@Data
public class StockInventoryReal implements Serializable {
    /**
     * 主键
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 商品ID
     */
    private Long goodsId;

    /**
     * 货品ID
     */
    private Long inventoryId;

    /**
     * 货品编码
     */
    private String inventoryCode;

    /**
     * 可售库存数
     */
    private Integer saleStockNum;

    /**
     * 占用库存数
     */
    private Integer usedStockNum;

    /**
     * 调拨在途数量
     */
    private Integer freeStockNum;

    /**
     * 乐观锁版本号
     */
    private Integer version;

    /**
     * 状态(1:初始化完成;0:初始化未完成)
     */
    private Integer status;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.UPDATE, update = "NOW()")
    private Date updateTime;

    /**
     * 是否逻辑删除
     */
    @TableLogic
    private Boolean deleted;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public Integer getMathSaleStockNum(Integer unrealSaleStockNum) {
        saleStockNum = ObjectUtils.defaultIfNull(saleStockNum, 0);
        unrealSaleStockNum = ObjectUtils.defaultIfNull(unrealSaleStockNum, 0);
        usedStockNum = ObjectUtils.defaultIfNull(usedStockNum, 0);
        return Math.max(saleStockNum + unrealSaleStockNum - usedStockNum, 0);
    }

    public Integer getMathFreeStockNum() {
        if (freeStockNum == null) return null;
        return Math.max(freeStockNum, 0);
    }

}